package com.sgb.kjwl.view.ui.activity.self;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.library.baseAdapters.BR;

import com.gyf.barlibrary.ImmersionBar;
import com.harisucici.jetpackbase.base.HBaseActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddressBinding;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.adapter.self.HUserAddressAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.AddressModel;
import com.swgk.core.util.LiuHaiPingUtils;

import org.greenrobot.eventbus.EventBus;

/**
 *
 */
public class AddressActivity extends HBaseActivity<ActivityAddressBinding, AddressModel> {

    //使用此字段判断从哪里跳转来的  1.集采-供方发起立即参与页面
    private int type;

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.getIsRefreshEnd(AddressActivity.this).observe(this, b -> {
            if (b) {
                binding.recyclerView.refreshComplete();
                if (viewModel.UserAddressEntityList.get().size() == 0) {
                    binding.nullView.setVisibility(View.VISIBLE);
                } else {
                    binding.nullView.setVisibility(View.GONE);
                }

            }
        });

        viewModel.getIsRefreshEnd(AddressActivity.this).observe(this, aBoolean -> {

        });
    }

    @Override
    public void initParam() {
        super.initParam();
       ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .fitsSystemWindows(false).transparentStatusBar().init();

        View liuHaiView = findViewById(com.swgk.core.R.id.liuhai_status_view);
        if (liuHaiView != null) {
            liuHaiView.getLayoutParams().height = LiuHaiPingUtils.getStatusBarHeight(this);
        }
    }

    @Override
    public void initData(View view) {
        Log.e("test_AddressActivity", "1");
        String tag = getIntent().getStringExtra("TAG");
        type = getIntent().getIntExtra("type", 0);
        binding.include.titleView.setText("地址管理");
        binding.include.ivBack.setOnClickListener(view1 -> finish());
        binding.include.rightView.setText("新增地址");

        binding.include.rightView.setVisibility(View.INVISIBLE);
        binding.include.rightView.setOnClickListener(view1 -> AddressAddActivity.start(this, null));
        binding.recyclerView.setLoadingMoreEnabled(false);

        HUserAddressAdapter hUserAddressAdapter = new HUserAddressAdapter(this,
                (binding, entity, i) -> {
                    //设置默认
                    entity.setDefaultAdress(true);
                    viewModel.overrideAddress(entity);
                },
                (binding, entity, i) -> {
                    //编辑
                    AddressAddActivity.start(AddressActivity.this, entity.getId());
                },
                (binding, entity, i) -> {
                    //删除
                    showDeleteDialog(entity.getId());
                }
        );

        binding.setAdapter(hUserAddressAdapter);

        //集采跳转过来选择开票信息
        if (type == 1) {
            hUserAddressAdapter.setItemClickCallBack(new HUserAddressAdapter.ItemClickCallBack() {
                @Override
                public void onItemClick(UserAddressEntity entity) {
                    LeaseEvent event = new LeaseEvent();
                    event.setMessageTag(EventConstant.EVENT_JICAI_FACTORY_LIJICANYU_MORENDIZHI);
                    event.setMessageData(entity);
                    EventBus.getDefault().postSticky(event);
                    finish();
                }
            });
        }
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_address;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    /**
     * 展示删除的dialog
     *
     * @param id
     */
    private void showDeleteDialog(final String id) {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(this, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("确定要删除地址吗？");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求删除接口
                viewModel.delIdAddress(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

}
